I will write about trading indicators in this post.
Both using technical analysis and fundamental analysis can help traders make informed decisions.
I will cover various types of indicators, with elaborate and fundamental math definitions.
Granularity | Typical Schema | Common Industry Term | Key Contents | Typical Uses |
---|---|---|---|---|
MBO (Market‑By‑Order, L3) | Schema::Mbo | Full order‑book depth / Level 3 | Add, cancel, and execution events for every individual order, including order_id | Market making, trade‑match replay, liquidity research |
MBP‑10 (Market‑By‑Price, L2) | Schema::Mbp10 | 10‑level price depth | Aggregated order size and updates at each of the top 10 price levels | Order‑book dynamics, arbitrage modeling |
MBP‑1 / TBBO (Top Book, L1) | Schema::Mbp1 | Best bid/ask | Best bid and ask quotes plus accompanying trades | Basic quote display, NBBO comparison |
Trades | Schema::Trades | Tick‑by‑tick trades / last sale | Every executed trade (price, size, aggressor side, etc.) | Price‑volume analysis, VWAP, trade‑driven strategies |
OHLCV‑T | Schema::Ohlcv | Bars / K‑line | Aggregated O‑H‑L‑C‑V data per second, minute, hour, or day | Backtesting, charting, low‑frequency signals |
We now only use OHLCV‑T data, the other data types are used for high-frequency trading and are not available in our current setup.
For a price (or data) series and window length :
indicator("ta.sma")
pine_sma(x, N) =>
sum = 0.0
for i = 0 to N - 1
sum := sum + x[i] / N
sum
plot(pine_sma(close, 15))
For a price series and look‑back length (often called the “period”):
Because , each older price is multiplied by a progressively smaller factor, so recent data dominate while the whole history still (theoretically) contributes.
indicator("ta.ema")
pine_ema(src, length) =>
alpha = 2 / (length + 1)
sum = 0.0
sum := na(sum[1]) ? src : alpha * src + (1 - alpha) * nz(sum[1])
plot(pine_ema(close,15))
Form | Meaning | Typical use |
---|---|---|
na (bare constant) | “Not‑available” — the Pine equivalent of NaN /null . | Initialize a series when you deliberately want the first bar(s) to be empty. |
na(x) (function) | Returns true if x is na , otherwise false . | Testing whether a previous‑bar value exists before you do math with it. |
nz()
means “not‑na, or zero”.
A simple moving average (SMA) assigns uniform weight inside its window and zero outside, so its lag to sudden jumps is fixed at roughly bars. Because EMA weights decay, its effective window shortens automatically when volatility rises: large moves add more weight to the newest bar and less to the stale tail, pulling the EMA closer to price. That “elastic” lag gives the EMA its characteristic tighter hug to the price compared with an SMA of equal period.
Why the EMA curve “looks like that”
Exponential-decay weighting
The exponential moving average (EMA) of a series with period can be written non-recursively as
The weights form a geometric (exponential) sequence that never quite reaches , so every past sample contributes, but each step back in time is worth a constant proportion ( ) less than the one before.
Visual effect: the curve bends smoothly toward new prices, but the bend gets shallower the farther the new price is from the old EMA, because the distant tail of small weights damps the response.
First-order low-pass filter
is the discrete-time equivalent of the differential equation of a simple RC low-pass filter:
whose impulse response is . That’s why the EMA curve has the same exponential relaxation shape when it pulls away from price spikes.
The MACD is a momentum oscillator that shows the relationship between two EMAs of a security’s price.
By tracking how a short‑term EMA “converges toward” or “diverges from” a longer‑term EMA, it highlights changes in trend strength, direction, and momentum. Gerald Appel introduced it in the late 1970s, and the classic “12‑26‑9” parameters (explained below) remain the default today.
Event | Typical reading | Notes |
---|---|---|
Signal‑line crossover | DIF crosses above DEA → potential buy; crosses below → potential sell | Akin to a two‑MA crossover system but applied to DIF vs. its own average. ([Wikipedia][1]) |
Zero‑line crossover | DIF moves from − to + → trend turns bullish; + to − → bearish | Confirms shifts in medium‑term trend direction. |
Histogram expansion / contraction | Growing bars = increasing momentum; shrinking bars = waning momentum | Gives an early visual cue before line crossovers. |
Price / MACD divergence | Price makes higher highs while DIF makes lower highs (or vice‑versa) | Can foreshadow trend reversals, but often produces early signals. |
https://en.wikipedia.org/wiki/MACD
The signal line is then built as the exponential moving average of the MACD line:
macd_val = ta.ema(close, 12) - ta.ema(close, 26)
signal = ta.ema(macd_val, 9)
hist = macd_val - signal
plot(macd_val, color=color.blue, title="MACD")
plot(signal, color=color.orange,title="Signal")
plot(hist, style=plot.style_histogram, title="Histogram")